/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Articulo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
 *
 * @author nan
 */
public class MedAgregarStock implements ActionListener, KeyListener{
    private Articulo.agregarStockGUI ventana = new Articulo.agregarStockGUI();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private String clave;
    private int a;
    private Articulo.articulosListar articuloLis;

    public MedAgregarStock(){
        ventana.setLocationRelativeTo(null);
        ventana.setActionListeners(this);
        ventana.setKeyListener(this);
        ventana.setVisible(true);
    }

    public MedAgregarStock(Articulo.articulosListar a){
        ventana.setLocationRelativeTo(null);
        ventana.setActionListeners(this);
        ventana.setKeyListener(this);
        ventana.setVisible(true);
        articuloLis = a;
        buscar(clave);
    }

    public void setCodigo(String a){
        clave = a;
    }

    public void actionPerformed(ActionEvent e) {
        buscar(clave);
        if(e.getSource()==ventana.getjButtonCancelar()){
            ventana.dispose();
        }
        if(e.getSource()==ventana.getjButtonGuardar()){
            agregar(clave,Integer.parseInt(ventana.getjTextFieldStockAgregar().getText()));
            ventana.getjTextFieldStockAgregar().setText("");
        }

    }

    public void agregar(String cod,int cant){
            conectar.conectar();
            Statement st = null;
            int g = a + cant;
            try {
                st = conectar.getConn().createStatement();
                String modifica = "UPDATE \"Articulo\" SET  \"Stock\"="+g+" WHERE \"codArt\" = '"+cod+"';";
                st.execute(modifica);
                st.close();
                ventana.dispose();
                articuloLis.actualizarTabla();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Nose pudo actualizar stock", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
            conectar.cerrar();
    }

    public void buscar(String codigo){
        clave = codigo;
        String[] titulos = {"Nombre", "Marca", "Descrip", "Stock Min", "Stock Max", "Stock"};
        Object[][] datos = {};
        conectar.conectar();
        Statement st;
        try {
            st = conectar.getConn().createStatement();
            String consulta = "SELECT * FROM \"Articulo\" WHERE \"codArt\" = '"+codigo+"';" ;
            ResultSet rs = st.executeQuery(consulta);
            String id = null;
            while (rs.next()){
                ventana.getjLabelCodigo().setText(codigo);
                ventana.getjLabelNombre().setText(rs.getString("nombre"));
                ventana.getjLabelMarca().setText(rs.getString("marca"));
                ventana.getjLabelStockActual().setText(rs.getString("Stock"));
                a = rs.getInt("Stock");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        ventana.dispose();
        conectar.cerrar();
    }

    public void keyTyped(KeyEvent e) {
        if (e.getSource()== ventana.getjTextFieldStockAgregar()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== ventana.getjTextFieldStockAgregar()){
            char caracter = e.getKeyChar();
            if(caracter == e.VK_ENTER){
                this.ventana.getjButtonGuardar().doClick();
            }
        }
    }

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }

    void setVisible(boolean b) {
        ventana.setVisible(b);
    }

}
